#!/bin/bash

run_sh_dir=""
start_shell="$1"
package_depends="$2"
apprun_cmd="$3"
wine_name=${apprun_cmd%%/bin*}
wine_name=${wine_name##*/}
export DEB_HOST_ARCH="$4"

RepalceVal()
{
    VALUE="$3"
    VALUE=$(echo ${VALUE//\\/\/})
    find "$1" -type f -exec sed -i "s#$2#$VALUE#" {} \;
}

TranslateTemplateFileUos()
{
    RepalceVal "$1" "@app_description@" "$app_description"
    RepalceVal "$1" "@app_name@" "$app_name"
    RepalceVal "$1" "@app_name_zh_cn@" "$app_name_zh_cn"
    RepalceVal "$1" "@deb_date@" "$deb_date"
    RepalceVal "$1" "@deb_packager@" "$deb_packager"
    RepalceVal "$1" "@deb_version_string@" "$deb_version_string"
    RepalceVal "$1" "@desktop_file_categories@" "$desktop_file_categories"
    RepalceVal "$1" "@desktop_file_main_exe@" "$desktop_file_main_exe"
    RepalceVal "$1" "@public_bottle_name@" "$public_bottle_name"
    RepalceVal "$1" "@old_package@" "$old_package_name"
    RepalceVal "$1" "@package_depends@" "$package_depends"
    RepalceVal "$1" "@host_arch@" "$DEB_HOST_ARCH"
    RepalceVal "$1" "@mime_type@" "$mime_type"
    RepalceVal "$1" "@activex_name@" "$activex_name"
    RepalceVal "$1" "@make_autostart@" "$make_autostart"
    RepalceVal "$1" "@send_to_desktop@" "$send_to_desktop"
    RepalceVal "$1" "@exec_path@" "$EXEC_PATH"
    RepalceVal "$1" "@install_setup@" "$install_setup"
    RepalceVal "$1" "@DISABLE_ATTACH_FILE_DIALOG@" "$DISABLE_ATTACH_FILE_DIALOG"
    RepalceVal "$1" "@DISABLE_FILEDLG_PLUGIN@" "$DISABLE_FILEDLG_PLUGIN"
    RepalceVal "$1" "@EXPORT_ENVS@" "$EXPORT_ENVS"
    RepalceVal "$1" "@deb_package_name@" "$deb_package_name"
    RepalceVal "$1" "@desktop_file_icon@" "$deb_package_name"
    RepalceVal "$1" "@run_sh_dir@" "$run_sh_dir"
    RepalceVal "$1" "@start_shell_path@" "$start_shell"
    RepalceVal "$1" "@apprun_cmd@" "$apprun_cmd"
    RepalceVal "$1" "@patch_loader@" "$patch_loader"
    RepalceVal "$1" "@version_info@" "$version_info"
    RepalceVal "$1" "@patch_version@" "$hack_patch_version"
    RepalceVal "$1" "@deb_install_size@" "$install_size"
    RepalceVal "$1" "@deb_download_size@" "$download_size"
}

echo "app run cmd:        $apprun_cmd"
echo "run wine cmd:        $wine_name"
echo "run shell path:      $start_shell"

if [[ "$package_depends" == *"libc6"* ]];then
    echo "wine依赖已改为公共runtime方式!!!"
    exit
fi

if [[ "$package_depends" != *"$wine_name"* ]];then
    echo "wine 依赖和执行的wine命令不匹配"
    exit
fi

curdir=`pwd`
dstdir="/tmp/final.dir"
outdir="$dstdir/opt/apps/$deb_package_name"
outentries="$outdir/entries"
outfiles="$outdir/files"
run_sh_dir="/opt/apps/$deb_package_name/files"
uos_package_save="$curdir/output/uos"

PKG_FILE="${deb_package_name}_${deb_version_string}_${DEB_HOST_ARCH}.deb"

echo "=====>Creating additional files..."
cd "$outdir"
if [ -d "$dstdir/debian" ]; then
    rm -rf "$dstdir/debian"
fi
cp -r "$curdir/debian/" "$dstdir"
TranslateTemplateFileUos "$dstdir/debian/"

if [ -d "$curdir/specified/$deb_package_name/root/" ]; then
    if [ -d "$dstdir/usr/" ]; then
        echo "$dstdir/usr/* /usr" &>> "$dstdir/debian/install"
    fi
    if [ -n "$install_setup" ];then
        md5sum "$outfiles/$install_setup" | awk '{print $1}' > "$outfiles/setup.md5sum"
    fi
fi

cp "$curdir/template/run.sh" "$outfiles"
cp "$curdir/template/pre_run.sh" "$outfiles"
TranslateTemplateFileUos "$outfiles/run.sh"

cp "$curdir/template/info" "$outdir"
TranslateTemplateFileUos "$outdir/info"

#if no desktop_file_categories, don't create desktop
if [ -n "$desktop_file_categories" ]; then
    mkdir -p "$outentries/applications"
    cp "$curdir/template/target.desktop" "$outentries/applications/$deb_package_name.desktop"
    TranslateTemplateFileUos "$outentries/applications/$deb_package_name.desktop"

    # TODO: We should generate icons from main program automatically.
    cd "$curdir/icons"
    find -iname "$desktop_file_icon" -exec dirname "{}" \; | xargs -I "{}" mkdir -p "$outentries/icons/{}"
    find -iname "$desktop_file_icon" -exec cp "{}" "$outentries/icons/{}" \;

fi

cp -r $curdir/config/ $dstdir
cp -r $curdir/xml/ $dstdir

cd "$dstdir"

echo "./opt/apps/$deb_package_name/ /opt/apps" &>> "debian/install"
echo "./config/pipelight-$deb_package_name /usr/share/pipelight/configs" &>> "debian/install"
echo "./xml/com.deepin.activex.$deb_package_name*.xml /usr/share/glib-2.0/schemas" &>> "debian/install"

echo "=====>Creating deb package..."
fakeroot "debian/build" "$dstdir"
echo "<=====done."

echo "=====> Removing temporary files..."
mv "$dstdir/$PKG_FILE" "$uos_package_save"
echo -e "\n\nDeb file generated: $PKG_FILE\n"
